//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.UI
{
    public class RibbonElement: FrameworkElement
    {

        static RibbonElement()
        {
            ControlStateProperty =
                DependencyProperty.RegisterAttached("ControlState", typeof (ControlState), typeof (RibbonElement),
                                                    new FrameworkPropertyMetadata(ControlState.Large,
                                                                                  FrameworkPropertyMetadataOptions.
                                                                                      Inherits |
                                                                                  FrameworkPropertyMetadataOptions.
                                                                                      AffectsArrange));
            ControlStateProperty.OverrideMetadata(typeof (ContextMenu),
                                                  new FrameworkPropertyMetadata(ControlState.Medium,
                                                                                FrameworkPropertyMetadataOptions.
                                                                                    Inherits |
                                                                                FrameworkPropertyMetadataOptions.
                                                                                    AffectsArrange));
        }


        public static bool GetIsIconVisible(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsIconVisibleProperty);
        }

        public static void SetIsIconVisible(DependencyObject obj, bool value)
        {
            obj.SetValue(IsIconVisibleProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsIconVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsIconVisibleProperty =
            DependencyProperty.RegisterAttached("IsIconVisible", typeof(bool), typeof(RibbonElement), new UIPropertyMetadata(true));



        public static IIconProvider GetIconProvider(DependencyObject obj)
        {
            return (IIconProvider)obj.GetValue(IconProviderProperty);
        }

        public static void SetIconProvider(DependencyObject obj, IIconProvider value)
        {
            obj.SetValue(IconProviderProperty, value);
        }

        // Using a DependencyProperty as the backing store for IconProvider.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconProviderProperty =
            DependencyProperty.RegisterAttached("IconProvider", typeof(IIconProvider), typeof(RibbonElement), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));


	
	

        public static double GetMaxTextWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(MaxTextWidthProperty);
        }

        public static void SetMaxTextWidth(DependencyObject obj, double value)
        {
            obj.SetValue(MaxTextWidthProperty, value);
        }

        // Using a DependencyProperty as the backing store for MaxTextWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxTextWidthProperty =
            DependencyProperty.RegisterAttached("MaxTextWidth", typeof(double), typeof(RibbonElement), new FrameworkPropertyMetadata(70.0d, FrameworkPropertyMetadataOptions.Inherits));



        public static TextWrapping GetTextWrapping(DependencyObject obj)
        {
            return (TextWrapping)obj.GetValue(TextWrappingProperty);
        }

        public static void SetTextWrapping(DependencyObject obj, TextWrapping value)
        {
            obj.SetValue(TextWrappingProperty, value);
        }

        // Using a DependencyProperty as the backing store for TextWrapping.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextWrappingProperty =
            DependencyProperty.RegisterAttached("TextWrapping", typeof(TextWrapping), typeof(RibbonElement), new FrameworkPropertyMetadata(TextWrapping.NoWrap, FrameworkPropertyMetadataOptions.Inherits));


        public static Style MediumControlStyle
        {
            get
            {
                if (_mediumControlStyle == null)
                {
                    _mediumControlStyle = new Style();
                    _mediumControlStyle.Setters.Add(new Setter(ControlStateProperty, ControlState.Medium));
                }
                return _mediumControlStyle;
            }
        }
        static Style _mediumControlStyle;

        public static Style SmallControlStyle
        {
            get
            {
                if (_smallControlStyle == null)
                {
                    _smallControlStyle = new Style();
                    _smallControlStyle.Setters.Add(new Setter(ControlStateProperty, ControlState.Small));
                }
                return _smallControlStyle;
            }
        }
        static Style _smallControlStyle;


        public static Style LargeControlStyle
        {
            get
            {
                if (_largeControlStyle == null)
                {
                    _largeControlStyle = new Style();
                    _largeControlStyle.Setters.Add(new Setter(ControlStateProperty, ControlState.Large));
                }
                return _largeControlStyle;
            }
        }
        static Style _largeControlStyle;

        public static object  GetIcon(DependencyObject obj)
        {
            return obj.GetValue(IconProperty);
        }

        public static void SetIcon(DependencyObject obj, object  value)
        {
            obj.SetValue(IconProperty, value);
        }

        // Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.RegisterAttached("Icon", typeof(object), typeof(RibbonElement), new UIPropertyMetadata(null));


	
        public static ControlState GetControlState(DependencyObject obj)
        {
            return (ControlState)obj.GetValue(ControlStateProperty);
        }
        
        public static void SetControlState(DependencyObject obj, ControlState value)
        {
            obj.SetValue(ControlStateProperty, value);
        }

        // Using a DependencyProperty as the backing store for ControlState.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControlStateProperty;




        // Using a DependencyProperty as the backing store for Pr.  This enables animation, styling, binding, etc...
        static readonly DependencyProperty previousStateProperty =
            DependencyProperty.RegisterAttached("PreviousState", typeof(PreviousState), typeof(RibbonElement), new UIPropertyMetadata(null));



        public static Style GetSmallGroupStyle(DependencyObject obj)
        {
            return (Style)obj.GetValue(SmallGroupStyleProperty);
        }

        public static void SetSmallGroupStyle(DependencyObject obj, Style value)
        {
            obj.SetValue(SmallGroupStyleProperty, value);
        }

        // Using a DependencyProperty as the backing store for SmallGroupStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SmallGroupStyleProperty =
            DependencyProperty.RegisterAttached("SmallGroupStyle", typeof(Style), typeof(RibbonElement), new UIPropertyMetadata(null));




        public static Style GetMediumGroupStyle(DependencyObject obj)
        {
            return (Style)obj.GetValue(MediumGroupStyleProperty);
        }

        public static void SetMediumGroupStyle(DependencyObject obj, Style value)
        {
            obj.SetValue(MediumGroupStyleProperty, value);
        }

        // Using a DependencyProperty as the backing store for MediumGroupStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MediumGroupStyleProperty =
            DependencyProperty.RegisterAttached("MediumGroupStyle", typeof(Style), typeof(RibbonElement), new UIPropertyMetadata(null));




        public static Style GetLargeGroupStyle(DependencyObject obj)
        {
            return (Style)obj.GetValue(LargeGroupStyleProperty);
        }

        public static void SetLargeGroupStyle(DependencyObject obj, Style value)
        {
            obj.SetValue(LargeGroupStyleProperty, value);
        }

        // Using a DependencyProperty as the backing store for LargeGroupStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LargeGroupStyleProperty =
            DependencyProperty.RegisterAttached("LargeGroupStyle", typeof(Style), typeof(RibbonElement), new UIPropertyMetadata(null));




        public static Style GetCollapsedGroupStyle(DependencyObject obj)
        {
            return (Style)obj.GetValue(CollapsedGroupStyleProperty);
        }

        public static void SetCollapsedGroupStyle(DependencyObject obj, Style value)
        {
            obj.SetValue(CollapsedGroupStyleProperty, value);
        }

        // Using a DependencyProperty as the backing store for CollapsedGroupStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CollapsedGroupStyleProperty =
            DependencyProperty.RegisterAttached("CollapsedGroupStyle", typeof(Style), typeof(RibbonElement), new UIPropertyMetadata(null));




        public static GroupState GetGroupState(DependencyObject obj)
        {
            return (GroupState)obj.GetValue(GroupStateProperty);
        }

        public static void SetGroupState(DependencyObject obj, GroupState value)
        {
            obj.SetValue(GroupStateProperty, value);
        }

        // Using a DependencyProperty as the backing store for GroupState.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GroupStateProperty =
            DependencyProperty.RegisterAttached("GroupState", typeof(GroupState), typeof(RibbonElement),
            new FrameworkPropertyMetadata(GroupState.Large,
            FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
            OnGroupStateChanged));

        static void OnGroupStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PreviousState previousState = (PreviousState)d.GetValue(previousStateProperty);
            if (previousState != null)
            {
                previousState.Restore(d);
                d.ClearValue(previousStateProperty);
            }
            Style Style = GetGroupStyle(d, (GroupState)e.NewValue);
            if (Style == null)
                return;
            d.SetValue(previousStateProperty, new PreviousState(d, Style));
            foreach (SetterBase setterBase in Style.Setters)
            {
                Setter setter = setterBase as Setter;
                if (setter == null)
                    continue;
                d.SetValue(setter.Property, setter.Value);
            }
        }
        static Style GetGroupStyle(DependencyObject d, GroupState state)
        {
            switch (state)
            {
                case GroupState.Large:
                    return GetLargeGroupStyle(d);
                case GroupState.Medium:
                    return GetMediumGroupStyle(d);
                case GroupState.Small:
                    return GetSmallGroupStyle(d);
                case GroupState.Collapsed:
                    return GetCollapsedGroupStyle(d);
            }
            throw new NotSupportedException();
        }




    }
}
